package com.whfc.mach.xxl.job;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.XxlJobConfig;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.util.DateUtil;
import com.whfc.mach.dao.AppMachDeviceMapper;
import com.whfc.mach.dto.AppMachInfo;
import com.whfc.mach.manager.MachDataManager;
import com.whfc.mach.manager.ObdDataMgr;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Description: 设备每日运行数据计算任务
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/29 16:46
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class MachDayDataStatJob {

    @Autowired
    private ObdDataMgr obdDataMgr;

    @Autowired
    private AppMachDeviceMapper appMachDeviceMapper;

    @Autowired
    private MachDataManager machDataManager;

    /**
     * 普通设备
     */
    @XxlJob("machDayData")
    public void machDayData() {
        try {
            Date now = DateUtil.getDate(new Date());
            Date yesterday = DateUtil.addDays(now, -1);
            XxlJobHelper.log("统计每天设备数据服务,date:{}", yesterday);
            int pageNum = 1;
            int pageSize = 20;
            int pages;
            do {
                PageHelper.startPage(pageNum, pageSize);
                List<AppMachInfo> machInfoList = appMachDeviceMapper.selectAppMachInfoList(DeviceType.mach.getValue());
                PageInfo<AppMachInfo> pageInfo = PageInfo.of(machInfoList);
                XxlJobHelper.log("task-statMachDayData,total:{},pages:{},pageNum:{},pageSize:{}", pageInfo.getTotal(), pageInfo.getPages(), pageNum, pageSize);
                pages = pageInfo.getPages();
                pageNum++;
                for (AppMachInfo machInfo : machInfoList) {
                    machDataManager.statMachDayData(machInfo, yesterday);
                }
            } while (pageNum <= pages);
        } catch (Exception ex) {
            XxlJobHelper.handleFail("统计设备每日运行数据,出现异常 error：" + ex.getMessage());
        }
    }

    /**
     * obd设备
     */
    @XxlJob("obdDayData")
    public void obdDayData() {
        try {
            Date now = DateUtil.getDate(new Date());
            Date yesterday = DateUtil.addDays(now, -1);
            XxlJobHelper.log("统计每天obd设备数据服务,date:{}", yesterday);
            int pageNum = 1;
            int pageSize = 20;
            int pages;
            do {
                PageHelper.startPage(pageNum, pageSize);
                List<AppMachInfo> machInfoList = appMachDeviceMapper.selectAppMachInfoList(DeviceType.obd.getValue());
                PageInfo<AppMachInfo> pageInfo = PageInfo.of(machInfoList);
                XxlJobHelper.log("task-statObdDayData,total:{},pages:{},pageNum:{},pageSize:{}", pageInfo.getTotal(), pageInfo.getPages(), pageNum, pageSize);
                pages = pageInfo.getPages();
                pageNum++;
                for (AppMachInfo machInfo : machInfoList) {
                    // 更新油耗和总放线里程
                    obdDataMgr.statMachDayData(machInfo, yesterday);
                    // 更新工时和工时分段
                    obdDataMgr.updateMachDaySegment(machInfo, yesterday);
                }
            } while (pageNum <= pages);
        } catch (Exception ex) {
            XxlJobHelper.handleFail("统计设备每日运行数据,出现异常 error：" + ex.getMessage());
        }
    }
}
